package cn.acewill.yxd.yunxundian.ui.user;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.model.Schedule;
import cn.acewill.yxd.yunxundian.model.Shop;
import cn.acewill.yxd.yunxundian.ui.MainActivity;

/**
 * Created by XXX on 2017/8/3.
 */

public class NoticeBaordCastReceiver extends BroadcastReceiver{

    protected YXDAPP yxdapp=new YXDAPP();
    protected DbManager db = x.getDb(yxdapp.getDaoConfig());
    @Override
    public void onReceive(Context context, Intent intent) {
        checkTaskComplete();
        showNotifiction(context);

        //再次开启服务
        Intent i = new Intent(context,MyNotifictionService.class);
        context.startService(i);
    }

    NotificationManager manager;
    int ID = 111;
    private void showNotifiction (Context context){
        manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        //此处有两种情况，第一，app还在进程中运行，二，app退出进程，需要进行判断，否者会出现数据混乱或bug
        Intent in = new Intent(context,CheckAppStatusBroadcast.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
        for (int i =0;i<taskList.size();i++){
            Schedule ss=taskList.get(i);
            try {
                Shop shop = db.selector(Shop.class).where("ID","=",ss.getShopID()).findFirst();
                if (shop!=null){
                    Notification.Builder builder = new Notification.Builder(context);
                    builder.setAutoCancel(true)
                            .setTicker("日程信息")
                            .setSmallIcon(R.mipmap.icon_logo)
                            .setContentTitle("日程信息"+"--"+ss.getSchStartTime().replace("T"," "))
                            .setContentText("最近未完成:"+ss.getTitle()+"-"+shop.getShopName())
                            .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
                            .setWhen(System.currentTimeMillis())
                            .setContentIntent(pi);
                    AudioManager am = (AudioManager) context
                            .getSystemService(Context.AUDIO_SERVICE);
                    Notification notification = builder.build();
                    manager.notify(ID+i,notification);
                }else{
                    Notification.Builder builder = new Notification.Builder(context);
                    builder.setAutoCancel(true)
                            .setTicker("日程信息")
                            .setSmallIcon(R.mipmap.icon_logo)
                            .setContentTitle("日程信息提示-重要")
                            .setContentText("最近未完成任务:"+ss.getTitle())
                            .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
                            .setWhen(System.currentTimeMillis())
                            .setContentIntent(pi);
                    Notification notification = builder.build();
                    manager.notify(ID+i,notification);
                }
            } catch (DbException e) {
                e.printStackTrace();
            }

        }

    }

    //判断当前的日程是否含有未完成的，时间未超过当前时间
    List<Schedule> taskList;
    int taskcount=0;
    DataProvider dp = new DataProvider();
    public void checkTaskComplete(){
        taskList = dp.getUndoScheduleSortByTime(MainActivity.id);
        if (taskList!=null){
            taskcount=taskList.size();
        }

    }

}
